// pages/tagSet/tagSet.js

let app = getApp()

Page({

  /**
   * 页面的初始数据
   */
  data: {
    tagList: null,
    active: 0
  },
  tapTag(e){
      let id = e.currentTarget.dataset.id
      let index = e.currentTarget.dataset.index
      console.log(index)
        this.data.tagList.forEach(val => {
            val.active = false
        })
      this.data.tagList[index].active = true
        this.setData({
            tagList: this.data.tagList
        })
  },
    addTag(){
        const that = this
        const alert = (content) => {
            $wuxDialog.alert({
                title: '提示',
                content: content,
            })
        }

        $wuxDialog.prompt({
            title: '提示',
            content: '密码为8位数字',
            fieldtype: 'number',
            password: !0,
            defaultText: '',
            placeholder: '请输入Wi-Fi密码',
            maxlength: 8,
            onConfirm:(e) => {
                const value = that.data.$wux.dialog.prompt.response
                // const content = value.length === 8 ? `Wi-Fi密码到手了: ${value}` : `请输入正确的Wi-Fi密码`
                // alert(content)
                app.globalData.post({
                    url: '/tags/create',
                    data: {
                        shopId: app.globalData.shopId,
                        name: value
                    },
                    success: (data) => {
                        this.setData({
                            tagList: [
                                {
                                    name: value,
                                    id: data.data.id
                                }, ...this.data.tagList]
                        })
                    }
                })
                // this.data.tagList.unshift({})
            },
        })
    },
    del(){
        wx.showModal({
            title: '提示',
            content: '确认删除所选标签？',
            success: (res) => {
                if (res.confirm) {
                    let activeId = this.data.tagList.filter(val => val.active)[0].id
                    let filter = this.data.tagList.filter(val => !val.active)
                    filter[0].active = true
                    this.setData({
                        tagList: filter
                    })
                    app.globalData.post({
                        url: '/tags/destroy',
                        data: {
                            tagId: activeId
                        },
                        success: (data) => {
                            
                        }
                    })
                    console.log('用户点击确定')
                } else if (res.cancel) {
                    console.log('用户点击取消')
                }
            }
        })
        
        
    },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
      app.globalData.post({
          url: '/tags/getTagsList',
          data: {
              shopId: app.globalData.shopId
          },
          success: (data) => {
              if(data.data && data.data.length){
                data.data[0].active = true
              }
              this.setData({
                  tagList: data.data
              })
          }
      })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
    
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
  
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
  
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
  
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
  
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
  
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
  
  }
})